package com.example.recuitactivity;


import androidx.fragment.app.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.recuitactivity.bean.Candidate;
import com.example.recuitactivity.helper.DbOpenHelper;


public class CandidateFragment extends Fragment {
    private View messageLayout;
    private EditText et_username;
    private EditText et_pwd;
    private EditText et_tele;
    private TextView tv_username_check;
    private TextView tv_pwd_check;
    private TextView tv_tele_check;
    private RadioGroup rg_sex;
    private Button btn_register;
    private DbOpenHelper dbOpenHelper;

    String sex="男";

    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.btn_register_candidate:
                    register();
                    break;
            }
        }
    };

    private RadioGroup.OnCheckedChangeListener listener2=new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            if(i==R.id.rb_male){
                sex="男";
            }else if(i==R.id.rb_female){
                sex="女";
            }
        }
    };

    public void initUI() {
        et_username=(EditText) messageLayout.findViewById(R.id.et_username_register_candidate);
        et_pwd=(EditText) messageLayout.findViewById(R.id.et_password_register_candidate);
        et_tele=(EditText) messageLayout.findViewById(R.id.et_tele_register_candidate);
        tv_username_check=(TextView) messageLayout.findViewById(R.id.tv_username_register_candidate_check);
        tv_pwd_check=(TextView) messageLayout.findViewById(R.id.tv_pwd_register_candidate_check);
        tv_tele_check=(TextView) messageLayout.findViewById(R.id.tv_tele_register_candidate_check);
        rg_sex=(RadioGroup) messageLayout.findViewById(R.id.rg_sex);
        btn_register=(Button) messageLayout.findViewById(R.id.btn_register_candidate);

        btn_register.setOnClickListener(listener);
        rg_sex.setOnCheckedChangeListener(listener2);
    }

    public void register(){
        dbOpenHelper=new DbOpenHelper(getActivity());

        String username = et_username.getText().toString();
        String password = et_pwd.getText().toString();
        String tele=et_tele.getText().toString();
        boolean register_successs = true;

        tv_username_check.setText("");
        tv_pwd_check.setText("");
        tv_tele_check.setText("");

        if (username == null || username.length() == 0) {
            register_successs = false;
            tv_username_check.setText("用户名不能为空！");
        } else if (dbOpenHelper.isExistCandidate(username)) {//用户名重复
            register_successs = false;
            tv_username_check.setText("该用户名已被注册！");
        }
        if(tele==null||tele.length()==0){
            register_successs = false;
            tv_tele_check.setText("电话不能为空");
        }
        if (password==null||password.length() ==0) {
            register_successs = false;
            tv_pwd_check.setText("密码不能为空！");
        }
        if (register_successs) {
            Candidate candidate = new Candidate(username,password,tele,sex,0);
            dbOpenHelper.insertCandidate(candidate);
            Toast.makeText(getActivity(),"恭喜你，注册成功！",Toast.LENGTH_LONG).show();
            getActivity().finish();
        } else {
            Toast.makeText(getActivity(), "注册失败，请检查注册信息！", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        messageLayout = inflater.inflate(R.layout.candidate_fragment, container, false);
        initUI();
        return messageLayout;
    }
}